import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;

public class MouseListenerPrimitive extends MouseAdapter implements MouseListener
{
	PickCanvas pickCanvas;
	MouseClicking _haupt;
	
	public MouseListenerPrimitive(Canvas3D importedCanvas, BranchGroup root, MouseClicking haupt)
	{
		_haupt = haupt;
		Canvas3D canvas = importedCanvas;
		pickCanvas = new PickCanvas(importedCanvas, root);
		pickCanvas.setMode(PickCanvas.GEOMETRY);
		canvas.addMouseListener(this);
	}
	
	public void mouseClicked(MouseEvent e)
	{
		//Defines the location on the canvas where the pick is to be performed.
		pickCanvas.setShapeLocation(e);
		PickResult result = pickCanvas.pickClosest();
		if (result == null)
		{
			System.out.println("Nothing Picked!");
			_haupt.allToRed();
		}
		else
		{
			Primitive p = (Primitive) result.getNode(PickResult.PRIMITIVE);
			
			if (p != null)
			{
				System.out.println(p.getClass().getName());
			}
			
			_haupt.changeMaterial(p.getClass().getName());
		}
	}
}
